<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>修改密码页面</title>
</head>
<body>

<div id="app">
    <h3 align="center">修改密码</h3>

    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" v-loading="loading">
        <el-form-item label="旧密码" prop="oldPass">
            <el-input v-model.number="ruleForm.oldPass"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>

</div>
<script th:src="@{/axios.min.js}"></script>
<script th:src="@{/vue.js}"></script>

<!-- 引入样式 -->
<link rel="stylesheet" th:href="@{/element-ui.css}">
<!-- 引入组件库 -->
<script th:src="@{/element-ui.js}"></script>
<script>
    new Vue({
        el: "#app",
        data() {
            var checkoldPass = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('旧密码不能为空'));
                }
                callback();


            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else if (value.length < 6 || value.length > 10) {
                    callback(new Error('密码长度6-10位'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    pass: '',
                    checkPass: '',
                    oldPass: ''
                },
                loading:false,
                rules: {
                    pass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ],
                    oldPass: [
                        {validator: checkoldPass, trigger: 'blur'}
                    ]
                }
            };
        },
        created() {

        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    let loginName = localStorage.getItem("loginName")
                    if (loginName.length == 0 || loginName == '') {
                        return false;
                    }
                    if (valid) {
                        this.loading=true
                        axios.put("/user/updatePass/" + this.ruleForm.oldPass + "/" + this.ruleForm.checkPass + "/" + loginName).then(resp => {
                            if (resp.data.flag){
                                this.loading=false
                                this.$message({
                                    message: resp.data.message,
                                    type: 'success'
                                });

                                setTimeout(
                                    function () {
                                        window.location.href="/index2"
                                    },1000
                                )
                            } else {
                                this.loading=false
                                this.$message.error(resp.data.message);
                            }
                        })

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    })


</script>
</body>
</html>